﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FuncVB;
using SAC.Elink;
using SAC.sqlHelper;
using BLL;

public partial class _Default : System.Web.UI.Page
{
    string errMsg = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //for (int i = 1; i < 3; i++)
        //{

        //        string sql = "insert into 机组运行小时数据表 (运行小时,日期,机组,设备类型) values (1,'" + DateTime.Parse("2012-07-13 10:00:00") + "'," + i + ",'发电机')";

        //        bool flag = SAC.sqlHelper.DBsql.RunNonQuery(sql, out errMsg);


        //}

        //Response.Redirect("~/bgData/Zbmsg.aspx?repName=cyd");

        Response.Redirect("~/bgData/Zbmsg.aspx?repName=gl");

        //Response.Redirect(Server.UrlEncode("../ednaview.aspx?fName=WHSIS.APP:GL\\汽水系统.RTV"));

        //Response.Redirect("~/RepAnyTime/Cydrb.aspx?repName=zh");
      

        //Response.Redirect("~/Rep/Cydnb.aspx?repName=cyd");

        //Response.Redirect("~/Rep/Cydzb.aspx?repName=zh&unit=1&repType=zr"); 

        //Response.Redirect("~/Rep/CydhbHB.aspx?repName=hx&unit=1");
        //Response.Redirect("~/Rep/CydymxHXHB.aspx?repName=hx&unit=1");
        
        
        //Response.Redirect("~/bgData/DLHBManger.aspx?dlType=qbb");
        // lsTrendURL = "ednaview_trend.aspx?NumValues=1&" & lsTagQueryString &"&StartTime=" & pStartTime & "&EndTime=" & pEndTime & "&Limits=" & Limits & "&LowerLimit=" & LowerLimit & "&UpperLimit=" & UpperLimit

        //DateTime dt = DateTime.Now;
        //double res = 0.00;

        //Func funcVB = new Func();

        ////Response.Write(dt.ToShortDateString() + dt.Day + dt.ToLocalTime());
        ////Response.Write(res.ToString("f" + "2"));

        //string errMsg = "";
        //string Hpoint = "";
        //// and 班值=&BZ&
        //DataTable dt = DBsql.RunDataTable("select * from T_sheet_sheetPara", out errMsg);

        //for (int i = 0; i < dt.Rows.Count; i++)
        //{
        //    Hpoint = dt.Rows[i]["小时点"].ToString();

        //    if (Hpoint != "")
        //    {
        //        string sql = "select 统计ID from 实时统计测点表 where 小时测点='" + Hpoint + "'";

        //        object obj = DBsql.RunSingle(sql, out errMsg);

        //        if (obj != null && obj.ToString() != "")
        //        {
        //            sql = "update T_sheet_ZsheetPara set 统计ID='" + obj.ToString() + "' where ID_KEY=" + dt.Rows[i]["ID_KEY"].ToString();

        //            bool flag = DBsql.RunNonQuery(sql, out errMsg);
        //        }
        //    }
        //}

        //DataTable dt = DBsql.RunDataTable("select * from T_sheet_ZsheetPara where 参数类型='SQL'", out errMsg);

        //for (int i = 0; i < dt.Rows.Count; i++)
        //{
        //    string yuju = dt.Rows[i]["SQL语句"].ToString();

        //    yuju += " and 班值=&BZ&";

        //    string sql = "update T_sheet_ZsheetPara set SQL语句='" + yuju + "' where ID_KEY=" + dt.Rows[i]["ID_KEY"].ToString();

        //    bool flag = DBsql.RunNonQuery(sql, out errMsg);


        //}

        ////string DL1 = "";
        ////string ID1 = "fdj1zxyg";
        ////string bm = "";
        ////object sDate = dt.ToShortDateString() + " " + "00:00:00";
        ////object eDate = dt.ToShortDateString() + " " + dt.Hour + ":00:00";

        ////funcVB.getdl(DL1, ID1, sDate, eDate, bm);


        ////        Response.Write(DL1);

        ////当月第一天
        //DateTime FirstDay = DateTime.Now.AddDays(-DateTime.Now.Day + 1);
        //Response.Write(FirstDay.ToString());
        //Response.Write(FirstDay.ToShortDateString());

        ////最后一天
        //DateTime LastDay = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day);
        //Response.Write(LastDay.ToString());

        ////上个月第一天
        //Response.Write("\n 上个月第一天：" + this.FirstDayOfPreviousMonth(DateTime.Now).ToString());
        ////上个月最后一天        
        //Response.Write("\n 上个月最后一天:" + this.LastDayOfPrdviousMonth(DateTime.Now).ToString());

        ////Response.Write(Convert.ToDateTime("2011-1-1 23:58:23").AddMonths(-1).ToString());
        ////Response.Write(DateTime.Now.AddYears(-1).ToLocalTime());

        ////Response.Write(DateTime.Now.ToLocalTime());
        ////Response.Write(DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01"));
        ////Response.Write(DateTime.Parse(DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString());

        //DateTime dt1 = DateTime.Parse("2011-08-01");



        //string r = this.DateDiff(dt1, DateTime.Now.Date);

        //string[] numDate = new string[int.Parse(r) + 1];

        //for (int i = 0; i < int.Parse(r) + 1; i++)
        //{
        //    Response.Write("\r\n 日期 :" + dt1.AddDays(+i).ToShortDateString());
        //}
        //Response.Write(numDate.Length);
        //Response.Write("/r/n++++" + r);
        //int k = dt.Hour - 1;
        //Response.Write("=====" + dt.ToShortDateString() + " " + k + "00:00");
        //Response.Write("========================="); 
        //Response.Write(this.FirstDayOfPreviousMonth(DateTime.Now.Date).ToShortDateString() + " " + "00:00:00"); //上月第一天

        //Response.Write(this.FirstDayOfPreviousMonth(DateTime.Now.Date).AddYears(-1).ToShortDateString() + " 00:00:00");

        //string p = "WHSIS.CALC.ZBTJH007";
        //string t = "2011-09-20";
        //DBedna db = new DBedna();
        //Response.Write("sdfafsadfa" + db.GetHisValueByTag(p, t));

        //Elink ek = new Elink();

        //ek.GetHisValue()

        //Response.Redirect("bgData/Zbmsg.aspx?repName=锅炉报表");

        //DateTime dt = DateTime.Parse("2012-2-20");

        //string v = this.GetInceDay(dt);
        // l = this.GetEndDay(dt);
        //string errMsg = "";

        //string sql = "select sum(统计值) from test2 where (统计ID = 'ZBTJ208') and 时间 between '2011-1-19 0:00:00' and  '2011-1-20 23:59:59' and 标记=1 and 机组=2";

        //object value = DBsql.RunSingle(sql, out errMsg);

        //string s = value.ToString();
        //if (value == null)
        //{
        //    Response.Write("OK");
        //}

        //if (value == "")
        //{
        //    Response.Write("meiy");
        //}


        //BLL.BLLRep br = new BLLRep();


        //  bool falg = br.Test();
        //string t = this.GetFHByTime("adf");
        //string s = this.GetPlantPowerx("1", "2", "3");

        //Response.Write(s);

    }

    public string GetPlantPowerx(string timeStr, string planCode, string powerCode)
    {
        timeStr = "2012-08-20 11:00:00";
        planCode = "";
        powerCode = "";
        //string ConnType = ConfigurationManager.AppSettings["method"].ToString();
        //string ConnStr = ConfigurationManager.AppSettings["access"].ToString();
        //TargetGetAndSet targetOpera = new TargetGetAndSet(ConnType, ConnStr);
        DateTime timeNow = DateTime.Now.AddMinutes(-5);
        DateTime timeToday = timeNow.Date;
        DateTime timePass = DateTime.Parse(timeStr);
        int todayCount = timeNow.Hour * 12 + timeNow.Minute / 5 + 1;
        if (timePass.Date < DateTime.Now.Date)
        {
            timeNow = timePass.AddDays(1).AddSeconds(-1);
            timeToday = timeNow.Date;
            todayCount = 288;
        }
        string plan = "[", today = "[", yesterday = "[";
        double[] planArr = new double[289];
        double[] todayArr = new double[todayCount];
        double[] yestArr = new double[289];
        for (int i = 0; i < 288; i++)
        {
            //TargetValue planValue = new TargetValue(planCode, timeToday);
            //targetOpera.GetTargetValue(planValue);
            planArr[i] = 1;
            plan += "1.00,";
            if (i < todayCount)
            {
                //TargetValue todayValue = new TargetValue(powerCode, timeToday);
                //targetOpera.GetTargetValue(todayValue);
                todayArr[i] = 2;// todayValue.DValue;
                today += "2.00,";
            }

            //TargetValue yestValue = new TargetValue(powerCode, timeToday.AddDays(-1));
            //targetOpera.GetTargetValue(yestValue);
            yestArr[i] = 3;// yestValue.DValue;
            yesterday += "3.00" + ",";// yestValue.DValue.ToString("F2") + ",";
            timeToday = timeToday.AddMinutes(5);
        }

        yesterday += todayArr[0].ToString("F2") + "]";
        plan += planArr[287].ToString("F2") + "]";

        if (timePass.Date < DateTime.Now.Date)
        {
            today += todayArr[287].ToString("F2") + "]";
        }
        else
        {
            today = today.Substring(0, today.Length - 1) + "]";
        }

        return "{\"plan\":" + plan + ",\"today\":" + today + ",\"yesterday\":" + yesterday + "}";
    }
    public string GetFHByTime(string timeStr)
    {
        int ret = 0;
        int count = 288;
        double val1 = 0;
        double val2 = 0;
        double val0 = 0;

        double valj1 = 0;
        double valj2 = 0;
        double valj0 = 0;

        string pdl = "";
        string fh1 = "WHSIS.U1APSH.U1A04013";
        string fh2 = "WHSIS.U2APSH.U2A04013";

        string zr1 = "[", zr2 = "[", zr0 = "[";
        string jr1 = "[", jr2 = "[", jr0 = "[";
        string date = "";
        Elink ek = new Elink();

        string dtZr = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 0:00:00")).AddDays(-1).ToString();
        string dtJr = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 0:00:00")).ToString();

        DateTime dtNow = DateTime.Now;

        for (int i = 0; i < count; i++)
        {
            date += dtJr + "==";
            ek.GetHisValue(fh1, dtZr, ref ret, ref val1);
            ek.GetHisValue(fh2, dtZr, ref ret, ref val2);
            val0 = val1 + val2;

            zr1 += val1.ToString("f2") + ",";
            zr2 += val2.ToString("f2") + ",";
            zr0 += val0.ToString("f2") + ",";

            if (DateTime.Parse(dtJr).AddMinutes(-5) < dtNow)
            {
                ek.GetHisValue(fh1, dtJr, ref ret, ref valj1);
                ek.GetHisValue(fh2, dtJr, ref ret, ref valj2);
                valj0 = valj1 + valj2;

                jr1 += valj1.ToString("f2") + ",";
                jr2 += valj2.ToString("f2") + ",";
                jr0 += valj0.ToString("f2") + ",";
            }
            else
            {

                Response.Write("adsf");
            }

            dtZr = DateTime.Parse(dtZr).AddMinutes(+5).ToString();
            dtJr = DateTime.Parse(dtJr).AddMinutes(+5).ToString();
        }

        zr1 = zr1.TrimEnd(',') + "]";
        zr2 = zr2.TrimEnd(',') + "]";
        zr0 = zr0.TrimEnd(',') + "]";

        jr1 = jr1.TrimEnd(',') + "]";
        jr2 = jr2.TrimEnd(',') + "]";
        jr0 = jr0.TrimEnd(',') + "]";
        //string plan = "[", today = "[", yesterday = "[";
        //string plan = "\"[0,0.2,0.47,0.55,1.38,1.38,1.38,1.38,1.48,1.5,1.89,2.0,1.94,1.91,1.75,1.6,0.6,0.35,0]\"", today = "\"tt\"", yesterday = "\"yy\"";
        //return "{\"plan\":" + plan + ",\"today\":" + today + ",\"yesterday\":" + yesterday + "}";
        return "{\"zr1\":" + zr1 + ",\"zr2\":" + zr2 + ",\"zr0\":" + zr0 + ",\"jr1\":" + jr1 + ",\"jr2\":" + jr2 + ",\"jr0\":" + jr0 + "}";
    }
    private void test()
    {
        //     If DateDiff("m", strtime3, Now) > 0 Then
        //    strMEnd = DateAdd("d", -1, Year(strtime3) & "-" & Month(strtime3) & "-01")
        // Else
        //    strMEnd = DateAdd("d", -1, Date)
        // End If

        //Select Case Month(strtime3)
        //       Case 1, 2, 3
        //            If Month(Date) > 3 Or Year(Date) > Year(strtime3) Then
        //               strJEnd = Year(strtime3) & "-3-31"
        //            Else
        //               strJEnd = DateAdd("d", -1, Date)
        //            End If
        //       Case 4, 5, 6
        //            If Month(Date) > 6 Or Year(Date) > Year(strtime3) Then
        //               strJEnd = Year(strtime3) & "-6-30"
        //            Else
        //               strJEnd = DateAdd("d", -1, Date)
        //            End If
        //       Case 7, 8, 9
        //            If Month(Date) > 9 Or Year(Date) > Year(strtime3) Then
        //               strJEnd = Year(strtime3) & "-9-30"
        //            Else
        //               strJEnd = DateAdd("d", -1, Date)
        //            End If
        //       Case 10, 11, 12
        //            If Year(Date) > Year(strtime3) Then
        //               strJEnd = Year(strtime3) & "-12-31"
        //            Else
        //               strJEnd = DateAdd("d", -1, Date)
        //            End If
        //       End Select


        // If DateDiff("y", strtime3, Now) > 0 Then
        //    strYEnd = DateAdd("d", -1, Year(strtime3) & "-01-01 00:00:00")
        // Else
        //    strYEnd = DateAdd("d", -1, Date)
        // End If

        //sql = "SELECT * FROM ADMINISTRATOR.壁温测点信息表 where 类型='水冷壁'";

        //DataTable dt = DBdb2.RunDataTable(sql, out errMsg);

        //int k = 0;

        //string tt = "";

        //for (int i = 0; i < dt.Rows.Count; i++)
        //{
        //    k = 1 + i;

        //    if (dt.Rows[i]["测点描述"].ToString().Contains("（前）"))
        //        tt = "水冷壁前";
        //    else if (dt.Rows[i]["测点描述"].ToString().Contains("（后）"))
        //        tt = "水冷壁后";
        //    else if (dt.Rows[i]["测点描述"].ToString().Contains("（左侧）"))
        //        tt = "水冷壁左";
        //    else if (dt.Rows[i]["测点描述"].ToString().Contains("（右侧）"))
        //        tt = "水冷壁右";

        //    sql = "update ADMINISTRATOR.壁温测点信息表 set 编号="+k+", typeinfo='"+tt+"'";

        //    DBdb2.
        //}
    }

    private string DateDiff(DateTime DateTime1, DateTime DateTime2)
    {
        string dateDiff = null;
        TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
        TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
        TimeSpan ts = ts1.Subtract(ts2).Duration();
        dateDiff = ts.Days.ToString();// +"天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
        return dateDiff;
    }

    /// <summary>
    /// 取得上个月第一天
    /// </summary>
    /// <param name="datetime">要取得上个月第一天的当前时间</param>
    /// <returns></returns>
    private DateTime FirstDayOfPreviousMonth(DateTime datetime)
    {
        return datetime.AddDays(1 - datetime.Day).AddMonths(-1);
    }

    /// <summary>
    /// 取得上个月的最后一天
    /// </summary>
    /// <param name="datetime">要取得上个月最后一天的当前时间</param>
    /// <returns></returns>
    private DateTime LastDayOfPrdviousMonth(DateTime datetime)
    {
        return datetime.AddDays(1 - datetime.Day).AddDays(-1);
    }



}
